package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang.math.NumberUtils;

import models.Point;
import models.User;
import play.mvc.Controller;
import play.mvc.With;
import services.JourneyService;
import services.UserService;

@With(Secure.class)
public class Journey extends Controller{
    @Inject
    private static JourneyService journeyService;
	@Inject
	private static UserService userService;
    
    public static void index(int id) {
        render(id);
    }
    
    public static void ajax(int journey_id) {
        List<Point> points = journeyService.getPointsByJourney(journey_id);
        renderJSON(points);
    }
    
    public static void ajax_insert() {
        String flag = params.get("flag");
        String lat 	= params.get("lat");
        String lng 	= params.get("lng");
        
        User user = userService.getUserByUsername(Secure.Security.connected());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int flag_int  = (NumberUtils.isNumber(flag)) ? Integer.valueOf(flag) : 0;
        
        models.Point point  = new models.Point();
        point.setFlag(flag_int);
        point.setLatitude(Float.valueOf(lat));
        point.setLongtitude(Float.valueOf(lng));
        point.setUser_id(user.getId().intValue());
        point.setStr_date(sdf.format(date));
        
        journeyService.insertPoint(point);
        renderJSON(point);
    }
    public static void  save_journey() {
		String jour_id= params.get("journey_id");
		String note = params.get("note");
		String name = params.get("name");
		int journey_id  = (NumberUtils.isNumber(jour_id)) ? Integer.valueOf(jour_id) : 0;
		models.Journey journey  = new models.Journey();
        journey.setJourney_id(journey_id);
        journey.setName(name);
        journey.setNote(note);
		journeyService.updateName(journey);
	}
    public static void  delete_journey() {
		String jour_id= params.get("journey_id");
		int journey_id  = (NumberUtils.isNumber(jour_id)) ? Integer.valueOf(jour_id) : 0;
		models.Journey journey  = new models.Journey();
        journey.setJourney_id(journey_id);
		journeyService.deleteJourney(journey);
	}
}
